/**************************************************
*XMLTree 2.0 Author jiangyi0115@163.com 2005-3-9
*2005-3-12 加入默认属性支持
*2005-3-13 加入动态加载支持
***************************************************
***************************************************
*将xml解析为htm树。
*支持 IE 5.0以上
*每个页面只支持一棵树
*
*xml 结构如下
*<xml id="treeData">
<tree showline="true"
dynamicload="true" dynamicxmlsourcepage="data.xml" queryattribute="nodeID"  defaultimage="Images/tree/icon-org.gif"
defaultclick="" defaultselect="" defaultexpland="true" >

<node text='AAA' image='Images/tree/icon-org.gif' href="#" checked="true">
		<node text='BBB'  expland='false' href="#" checked="true" onclick='alert'/>
</node>

<node text='CCC' image='Images/tree/icon-org.gif' checked="true" onclick="alert('click!')" style="cursor:hand">
	<node text='DDD' expland='true' checked="true"/>
	<node text='EEE' checked="false"/>
</node>

<node text='FFF' expland='false' checked="true"/>
</node>

</tree>
</xml>
*
*
节点名必须是node
不能有nodeID属性(xml解析时,会自动生成).
每个节点将会生成一个<a>标签,其 ID 属性为 node_ 加节点 ID 

文档节点支持以下属性
1.
加载方式：
dynamicload="true"	可选 是否采用动态加载
dynamicxmlsourcepage="data.xml" 可选 动态加载页面路径
queryattribute="nodeID" 可选 动态加载请求数据时在加载页面路径附加的查询参数，将从节点的属性值中取

2。
showline="true" 可选 是否显示下拉线 

3.节点默认属性：若节点未指定相应属性，则会添加默认属性。
 defaultclick		可选 节点默认单击函数 参数同 onclick
 defaultselect		可选 节点默认选择事件函数 参数同 onselect
 defaultexpland		可选 节点默认折叠状态
 defaultimage		可选 节点默认图标
 defaultchecked		可选 节点默认选中状态

支持以下属性：(全部小写)
text	必选 ，	节点文字
expland 可选	= true 或者false 节点是否展开 ，上层节点的展开状态将受下层节点影响。
image	可选	节点前的图标，若无此属性 ，则显示默认图标。
checked 可选	= true 或者false 节点是否选中 ，当存在此属性时 ，会在节点前显示一个复选框。
onclick 可选    节点单击事件函数，可以是函数全部，也可以只有函数名，若只有函数名，解析时会自动加上nodeID(节点序号) ,
				可利用此来实现对相关元素如子节点,夫节点,复选框的控制.
onselect 可选	节点前复选框单击函数, 可以是函数全部，也可以只有函数名，若只有函数名，解析时会自动加上nodeID(节点序号) ,
				可利用此来实现对相关元素如子节点,夫节点,复选框的控制.

//动态加载时
haschildnodes="true" 是否是末端节点

其他属性 会保留.
如: name, href , style ,class, target , onmouseover , onmouseup  ...
*
*
提供的接口函数:
getNodeByID(id);			//通过节点id获取节点xml对象　XmlNode 
checkParentNodesByID( id )	//选中id指定的节点的所有父节点
checkChildNodesByID( id )	//选中id指定的节点的所有子节点
getCheckboxObj(id)          //获取复选框对象
checkCheckboxObj(id,checked)//设置复选框对象选择状态

//重新设置选择状态
//keyAttribute 设置依据字段
//valueList   以","间隔的值列表
function setCheckedStatus(keyAttribute,valueList)

//获取选择的节点的某个属性值列表
//keyAttribute 要获取的属性名
function getCheckedValueList(keyAttribute)

//清除所有节点的选择状态
function clearCheckedStatus()

*****************************************************/

//图片路径
var plusImgSrc = "Images/tree/plus.gif";
var minusImgSrc = "Images/tree/minus.gif";

var centerPlusImgSrc = "Images/tree/centerplus.gif";
var centerMinusImgSrc = "Images/tree/centerminus.gif";

var endPlusImgSrc = "Images/tree/endplus.gif"; 
var endMinusImgSrc = "Images/tree/endminus.gif"; 

var background = "Images/tree/back.gif";

var nodeImgSrc = "Images/tree/node.gif";
var endNodeImgSrc = "Images/tree/end.gif";
//
var tdIDtag = "nodeChildren_";	//子节点tr id前缀
var imgIDtag = "nodeImg_";		//折叠展开图片id前缀

var xmlDoc; //全局xml对象
var nodeList; //节点集合

//默认参数
var defaultclick ;
var defaultselect ;
var defaultexpland ;
var defaultimage ;
var defaultchecked ;
//动态加载属性
var dynamicload = "true" ;
var dynamicxmlsourcepage = "" ;
var queryattribute = "" ;
var showline ;

//
var textAttributeName = "text";

function setTextAttributeName( newName ){
	textAttributeName = newName ;
}

function setImagePath(imagepath){

	plusImgSrc = imagepath + "tree/plus.gif";
	minusImgSrc = imagepath +  "tree/minus.gif";

	centerPlusImgSrc = imagepath +  "tree/centerplus.gif";
	centerMinusImgSrc = imagepath +  "tree/centerminus.gif";

	endPlusImgSrc = imagepath +  "tree/endplus.gif"; 
	endMinusImgSrc = imagepath +  "tree/endminus.gif"; 

	background = imagepath +  "tree/back.gif";

	nodeImgSrc = imagepath +  "tree/node.gif";
	endNodeImgSrc = imagepath +  "tree/end.gif";

}
 
//
//解析xml
//参数：xmlIslandObjOrPath xml数据岛屿或xml文件路径
//参数：showLine 是否显示下拉先 默认值 false
function parseTreeXml(xmlIslandObjOrPath){
	if( xmlIslandObjOrPath == null ){
		alert("对象不在");
		return "";
	}
	
	xmlDoc = new ActiveXObject("microsoft.xmlDOM");
	xmlDoc.async = false; 
	if( typeof(xmlIslandObjOrPath)=="object"  ){
		if( xmlIslandObjOrPath.innerHTML == null ){
			alert( "xmlIsland为null或内容为空！" );
			return "";
		}
		try{

			xmlDoc.loadXML(xmlIslandObjOrPath.innerHTML);
		}catch(e){
			alert(e);
		}
	}else{
	 
		try{
			xmlDoc.load( xmlIslandObjOrPath );
			//alert(xmlDoc.xml);
		}catch(e){
			alert(xmlDoc.parseError.reason);
		}
	}
	if(xmlDoc.parseError!=0){
		alert( "xml解析错误！" + xmlDoc.parseError.line +"行：\n"+ xmlDoc.parseError.reason );
		return "";
	}

	var root = xmlDoc.documentElement;
//	alert(xmlDoc.xml);

	//加载 默认参数
	defaultclick = root.getAttribute("defaultclick");
	if( defaultclick == "" ) defaultclick = null;

	defaultselect = root.getAttribute("defaultselect");
	if( defaultselect == "" ) defaultselect = null;

	defaultexpland = root.getAttribute("defaultexpland");
	if( defaultexpland == "" ) defaultexpland = null;

	defaultimage = root.getAttribute("defaultimage");
	if( defaultimage == "" ) defaultimage = null;

	defaultchecked = root.getAttribute("defaultchecked");
	if( defaultchecked == "" ) defaultchecked = null;

	dynamicload = root.getAttribute("dynamicload");
	if( dynamicload == "" ) dynamicload = null;

	dynamicxmlsourcepage = root.getAttribute("dynamicxmlsourcepage");
	if( dynamicxmlsourcepage == "" ) dynamicxmlsourcepage = null;

	queryattribute = root.getAttribute("queryattribute");
	if( queryattribute == "" ) qureyattribute = null;

	showline = root.getAttribute("showline");
	if( showline == "" ) showline = null;
	
	setNodeID(xmlDoc,root); 

	nodeList = root.getElementsByTagName("*");

	imagePath = root.getAttribute("imagepath");
	if( imagePath != null && imagePath != "" )
		setImagePath( imagePath );

	textAttName = root.getAttribute("textattributename");
	if( textAttName != null && textAttName != "" )
		setTextAttributeName( textAttName );


	if( dynamicload == "true" ){
		return initDxmlTree( );
	}else{
		if( showline == "true" )
			return getNodeHTMLWithLine(xmlDoc.documentElement );
		else
			return getNodeHTML(xmlDoc.documentElement );
	}

/*	if( showLine ) return getNodeHTMLWithLine(xmlDoc.documentElement );
	else	return getNodeHTML(xmlDoc.documentElement );

 */
 
}
//通过节点id获取节点xml对象
function getNodeByID(id){
	var i;
	nodeList = xmlDoc.documentElement.getElementsByTagName("*"); //////////
	for(i=0;i<nodeList.length;i++){
		if(nodeList[i].getAttribute("nodeID")==id) return nodeList[i] ;
	 }
	 return null;
}

//初始化节点id
var nodeID = 0;
function setNodeID(xmlDoc,node){
	var i = 0;
	var newAtt;
	for( i = 0; i < node.childNodes.length ; i ++ ){ 
		newAtt = xmlDoc.createAttribute("nodeID");
		nodeID ++;
		newAtt.value = nodeID ;
		node.childNodes[i].attributes.setNamedItem( newAtt ) ;
		setNodeID(xmlDoc,node.childNodes[i]);
	}
}

function getNodeCount(){
	return nodeID;
}

function getImgFileName( path ){
	var arr = path.split( "/" );
	if( arr.legnth < 1 ) return path ;
	
	return arr[ arr.length - 2 ] + "/" + arr[ arr.length - 1 ];
}
//折叠展开函数
function menu(id){
 
	var objTD = null ;
	try{
		objTD = eval( tdIDtag + id );	
		var imgID = imgIDtag + id ;	
	}catch(e){alert(objTD);}

	if( objTD == null ) return ;
				
	if( objTD == null ) return;
				 
	if( objTD.style.display == "none" )
		objTD.style.display = "";
	else
		objTD.style.display = "none";
		
	_plusImgSrc = getImgFileName( plusImgSrc );	
	_minusImgSrc = getImgFileName( minusImgSrc );
	_endPlusImgSrc = getImgFileName( endPlusImgSrc );
	_endMinusImgSrc = getImgFileName( endMinusImgSrc );
	_centerPlusImgSrc = getImgFileName( centerPlusImgSrc );
	_centerMinusImgSrc = getImgFileName( centerMinusImgSrc );
		
		

	var imgSrc = document.images[ imgID ].src ;
	//简单树
	if( imgSrc.indexOf(_plusImgSrc) > -1  ){
		document.images[ imgID ].src = minusImgSrc ;
		return ;
	}
	if( imgSrc.indexOf(_minusImgSrc) > -1  ){
		document.images[ imgID ].src = plusImgSrc ;
		return ;
	}
	 //有下拉线时 
	if( imgSrc.indexOf(_endPlusImgSrc) > -1  ){
		document.images[ imgID ].src = endMinusImgSrc ;
		return ;
	}
	if( imgSrc.indexOf(_endMinusImgSrc) > -1  ){
		document.images[ imgID ].src = endPlusImgSrc ;
		return ;
	}
//
	if(imgSrc.indexOf(_centerPlusImgSrc) > -1  ){
		document.images[ imgID ].src = centerMinusImgSrc ;
		return ;
	}
	if( imgSrc.indexOf(_centerMinusImgSrc) > -1  ){
		document.images[ imgID ].src = centerPlusImgSrc ;
		return ;
	}

}

//解析当前节点,返回节点的<a>标签表示
function getNodeText( node ){
	var text = node.getAttribute( textAttributeName );
	var id = node.getAttribute("nodeID");
	 
	var j = 0;
	var f = "<a id='node_" + id + "' ";
	var name = "",value = "" ;
	//处理节点的onclick属性
	var onclick = node.getAttribute("onclick");
	if( onclick != null ){
		if( onclick.indexOf("(") != -1 && onclick.indexOf(")") != -1 )
			f += " onclick=\"" + onclick + "\" ";
		else
			f += " onclick=\"" + onclick + "(" + id + ")" + "\" ";
	}else{ //默认单击
		if( defaultclick != null ){
			if( defaultclick.indexOf("(") != -1 && defaultclick.indexOf(")") != -1 )
			f += " onclick=\"" + defaultclick + "\" ";
		else
			f += " onclick=\"" + defaultclick + "(" + id + ")" + "\" ";
		}
	}

	for(j=0;j<node.attributes.length;j++ ){
		name = node.attributes[j].name ;
		value = node.attributes[j].value ;
		if( name == "checked" || name == "onclick" || name == "nodeID" || name == "text" || name == "expland" || name == "image" ||  name == "onselect" ){

		}else{
		  f += name +"=\""+ value + "\" " ;
		}
	}
	return f + ">" + text + "</a>" ;
}

function getCheckboxHTML( node ){
	var check = node.getAttribute("checked");
	if( check == null  ){
		if( defaultchecked != null ){  //默认选择
			check = defaultchecked ;
		}else{
			return "";
		}
	}

	if( check== "true" ) check = "checked";
	else check = "";
	
	//alert( check ) ;

	var onselect = node.getAttribute("onselect");
	var id = node.getAttribute("nodeID");
	if( onselect == null ){
		if( defaultselect != null  ){ //默认选择事件
			if( defaultselect.indexOf("(") > -1 ) 
				return "<input type='checkbox' name='checkbox_" +id+ "' onclick=\"" +defaultselect+ "\" " +check+ " class='tree_selector' >";
			else 
				return "<input type='checkbox' name='checkbox_" +id+ "' onclick=\"" +defaultselect+ "(" +id+ ")\" " +check+ " class='tree_selector'  >";
		}else{
			return "<input type='checkbox' name='checkbox_" + id + "' class='tree_selector' " +check+ " >";
		}
	}
	if( onselect.indexOf("(") > -1 ) 
		return "<input type='checkbox' name='checkbox_" +id+ "' onclick=\"" +onselect+ "\" " +check+ " class='tree_selector'>";
	else 
		return "<input type='checkbox' name='checkbox_" +id+ "' onclick=\"" +onselect+ "(" +id+ ")\" " +check+ "  class='tree_selector'>";
}

function getDefaultImg(){
	if( defaultimage != null )
		return  "<img  src='"+defaultimage+"' border='0'>" ;  
	else
		return "";
}

//解析当前节点,生成html
//无下拉线
function getNodeHTML( node ){
	if(  node.childNodes.length == 0 ) return "&nbsp; " ;
	var html = "<table border=0 cellspacing=0 cellpadding=0>";
	var img = "";
	var nodeClick = "";
	var nodeDisplay = "";
	var i = 0 ;
	var n = null;
	
	var clickImg = "";
	var subHTML = "" ;
	var idRangeMin = 0;
	var currentNodeID = 0;
	var name = "";
	for( i = 0; i < node.childNodes.length ; i ++ ){ 
		n = node.childNodes[i];
		currentNodeID =  n.getAttribute("nodeID") ;
		name = getNodeText(n);
		//节点图片
		img = n.getAttribute("image") ;
		if( img !=  null && img != "" )
			img = "<img  src='"+img+"' border='0'>" ;
		else
			img = getDefaultImg();
		//是否展开
		if( isExpland(n) ){
			nodeDisplay = "";
			clickImg =  "<img onclick=\"menu('" + currentNodeID + "')\"  id='" + imgIDtag + currentNodeID + "' src='" + minusImgSrc + "' border='0'>"
		}else{
			nodeDisplay = "none";
			clickImg =  "<img onclick=\"menu('" + currentNodeID + "')\"   id='" + imgIDtag + currentNodeID + "' src='" + plusImgSrc + "' border='0'>"
		}

		html += "<tr><td width='15'>";

		if( n.childNodes.length > 0 )
		 	html += clickImg ;
		else 
			html += "&nbsp;";

		//
		 html += "</td><td nowrap>" ;
 

		html += getCheckboxHTML(n);

		
		html += img + name + "</td></tr>"; 

		if(  n.childNodes.length > 0 ){
			html += "<tr style='display:" + nodeDisplay + "' id='" + tdIDtag + currentNodeID + "'><td >&nbsp;</td><td>";
			html += getNodeHTML( n ) ;
			html += "</td></tr>";
		}
	}
	html += "</table>"; 
	return html;
}
//**************************************************************

//**************************************************************

//解析当前节点,生成html
//有下拉线
function getNodeHTMLWithLine( node ){
	if(  node.childNodes.length == 0 ) return "&nbsp; " ;
	var html = "<table border=0 cellspacing=0 cellpadding=0>";
	var img = "";
	var nodeClick = "";
	var nodeDisplay = "";
	var i = 0 ;
	var n = null;
	
	var clickImg = "";
	var subHTML = "" ;
	var idRangeMin = 0;
	var currentNodeID = 0;
	var name = "";
	for( i = 0; i < node.childNodes.length - 1 ; i ++ ){ 
		n = node.childNodes[i];
		currentNodeID =  n.getAttribute("nodeID") ;
		name = getNodeText(n);
		//节点图片
		img = n.getAttribute("image") ;
		if( img !=  null && img != "" )
			img = "<img src='"+img+"' border='0'>" ;
		else
			img = getDefaultImg();

		//是否展开
		if( isExpland(n) ){
			nodeDisplay = "";
			clickImg =  "<img  onclick=\"menu('" + currentNodeID + "')\" align='left' id='" + imgIDtag + currentNodeID + "' src='" + centerMinusImgSrc + "' border='0'>"
		}else{
			nodeDisplay = "none";
			clickImg =  "<img   onclick=\"menu('" + currentNodeID + "')\" align='left' id='" + imgIDtag + currentNodeID + "' src='" + centerPlusImgSrc + "' border='0'>"
		}

		html += "<tr><td>";

		if( n.childNodes.length > 0 )
		 	html += clickImg ;
		else 
			html +=  "<img align='left'  src='" + nodeImgSrc + "' border='0'>"

		//html += getCheckboxHTML(n);

		html += "</td><td nowrap>" ;
		
		html += getCheckboxHTML(n);
		
		html +=  img + name + "</td></tr>"; 

		if( n.childNodes.length > 0 ){
			html += "<tr style='display:" + nodeDisplay + "' id='" + tdIDtag + currentNodeID + "'><td style='background-repeat:repeat-y;BACKGROUND-POSITION-X: center;'  " ;  
			html += " background='"+background+"' " ;
			html += " width='5'></td><td>";
			html += getNodeHTMLWithLine( n );
			html += "</td></tr>";
		}
	}

	//解析最后一个节点
		n = node.childNodes[i];
		currentNodeID =  n.getAttribute("nodeID") ;
		name = getNodeText(n);
		//节点图片
		img = n.getAttribute("image") ;
		if( img !=  null && img != "" )
			img = "<img  src='"+img+"' border='0'>" ;
		else
			img = getDefaultImg();

		//是否展开
		if( isExpland(n) ){
			nodeDisplay = "";
			clickImg =  "<img  onclick=\"menu('" + currentNodeID + "')\" align='left' id='" + imgIDtag + currentNodeID + "' src='" + endMinusImgSrc + "' border='0'>"
		}else{
			nodeDisplay = "none";
			clickImg =  "<img   onclick=\"menu('" + currentNodeID + "')\" align='left' id='" + imgIDtag + currentNodeID + "' src='" + endPlusImgSrc + "' border='0'>"
		}

		html += "<tr><td>";

		if( n.childNodes.length > 0 )
		 	html += clickImg ;
		else 
			html +=  "<img align='left'  src='" + endNodeImgSrc + "' border='0'>"

		//check = n.getAttribute( "checked" );

		//html += getCheckboxHTML(n);

		html += "</td><td nowrap>" ;
		
		html += getCheckboxHTML(n);
		
		html += img + name + "</td></tr>"; 

		if( n.childNodes.length > 0 ){
			html += "<tr style='display:" + nodeDisplay + "' id='" + tdIDtag + currentNodeID + "'><td style='background-repeat:repeat-y;'  " ;  
			//html += " background='"+background+"' " ;
			html += " width='5'></td><td>";
			html += getNodeHTMLWithLine( n );
			html += "</td></tr>";
		}

	html += "</table>"; 
	return html;
}

//*********************************************************

//***********************************************************************************************************
//动态加载xml解析 
function initDxmlTree(){
	//alert(queryattribute);
	if( showline == "true" )
		return parseNodeWithLine( xmlDoc.documentElement ) ;
	else
	  return parseNode( xmlDoc.documentElement ) ;
 }

function parseNode( node ){
	if(  node.childNodes.length == 0 ) return "" ;
	var html = "<table border=0 cellspacing=0 cellpadding=0>";
	var img = "";
	var nodeClick = "";
	var nodeDisplay = "";
	var i = 0 ;
	var n = null;
	
	var clickImg = "";
	var subHTML = "" ;
	var idRangeMin = 0;
	var currentNodeID = 0;
	var name = "";
	var haschildnodes = null ;
	var queryattributevalue ;
	 
	for( i = 0; i < node.childNodes.length ; i ++ ){ 
		n = node.childNodes[i];
		currentNodeID =  n.getAttribute("nodeID") ;
		name = getNodeText(n);
		queryattributevalue =  n.getAttribute( queryattribute ) ;
		//节点图片
		img = n.getAttribute("image") ;
		if( img !=  null && img != "" )
			img = "<img src='"+img+"' border='0'>" ;
		else
			img = getDefaultImg();

		haschildnodes = n.getAttribute("haschildnodes") ;

		if( haschildnodes == null || haschildnodes == "true" ){
			if( true == isExpland(n) &&  n.childNodes.length > 0 ){//是否展开
				nodeDisplay = "";
				clickImg =  "<img onclick=\"dload('" + currentNodeID + "','" +queryattributevalue+"')\" id='" + imgIDtag + currentNodeID + "' src='" + minusImgSrc + "' border='0'>" ;
			}else{
				nodeDisplay = "none";
				clickImg =  "<img onclick=\"dload('" + currentNodeID + "','" +queryattributevalue+"')\" id='" + imgIDtag + currentNodeID + "' src='" + plusImgSrc + "' border='0'>" ;
			}
		}else{
			nodeDisplay = "none";
			clickImg =  "" ;
		}

		html += "<tr><td id='imgtd_" + currentNodeID + "'>";
		html += clickImg ;
		
		html += "</td><td nowrap>" ;
		
		html += getCheckboxHTML(n);
		
		html += img + name + "</td></tr>"; 

			html += "<tr style='display:" + nodeDisplay + "' id='" + tdIDtag + currentNodeID + "'><td>&nbsp;</td>" ;
			html += "<td id='cc_" + currentNodeID +  "'>";
			html += parseNode( n ) ;
			html += "</td></tr>";
	}
	html += "</table>"; 
	return html;
}


function parseNodeWithLine( node ){
	if(  node.childNodes.length == 0 ) return "" ;
	var html = "<table border=0 cellspacing=0 cellpadding=0>";
	var img = "";
	var nodeClick = "";
	var nodeDisplay = "";
	var i = 0 ;
	var n = null;
	
	var clickImg = "";
	var subHTML = "" ;
	var idRangeMin = 0;
	var currentNodeID = 0;
	var name = "";
	var haschildnodes = null ;
	var queryattributevalue ;
	var haschildnodes ; 
	for( i = 0; i < node.childNodes.length - 1 ; i ++ ){ 
		n = node.childNodes[i];
		currentNodeID =  n.getAttribute("nodeID") ;
		name = getNodeText(n);
		queryattributevalue =  n.getAttribute( queryattribute ) ;
		//节点图片
		img = n.getAttribute("image") ;
		if( img !=  null && img != "" )
			img = "<img  align='left' src='"+img+"' border='0'>" ;
		else
			img = getDefaultImg();

		haschildnodes = n.getAttribute("haschildnodes") ;

		if( haschildnodes == null || haschildnodes == "true" ){
			if( true == isExpland(n) &&  n.childNodes.length > 0 ){//是否展开
				nodeDisplay = "";
				clickImg =  "<img align='left' onclick=\"dload('" + currentNodeID + "','" +queryattributevalue+"')\" id='" + imgIDtag + currentNodeID + "' src='" + centerMinusImgSrc + "' border='0'>" ;
			}else{
				nodeDisplay = "none";
				clickImg =  "<img align='left' onclick=\"dload('" + currentNodeID + "','" +queryattributevalue+"')\" id='" + imgIDtag + currentNodeID + "' src='" + centerPlusImgSrc + "' border='0'>" ;
			}
		}else{
			nodeDisplay = "none";
			clickImg = "<img src='" + nodeImgSrc + "' border='0'>" ;
		}

		html += "<tr><td id='imgtd_" + currentNodeID + "'>";
		html += clickImg ;
		
		html += "</td><td nowrap>" ;
		
		html += getCheckboxHTML(n);
		
		html += img + name + "</td></tr>"; 

		html += "<tr style='display:" + nodeDisplay + "' id='" + tdIDtag + currentNodeID + "'><td style='background-repeat:repeat-y;' " ;
		html += " background='"+background+"' " ;
		html += ">&nbsp;</td>" ;
		html += "<td id='cc_" + currentNodeID +  "'>";
		html += parseNodeWithLine( n ) ;
		html += "</td></tr>";
	}

	//parse the last node
	    n = node.childNodes[i];
		currentNodeID =  n.getAttribute("nodeID") ;
		name = getNodeText(n);
		queryattributevalue =  n.getAttribute( queryattribute ) ;
		//节点图片
		img = n.getAttribute("image") ;
		if( img !=  null && img != "" )
			img = "<img  align='left' src='"+img+"' border='0'>" ;
		else
			img = getDefaultImg();

		haschildnodes = n.getAttribute("haschildnodes") ;

		if( haschildnodes == null || haschildnodes == "true" ){
			if( true == isExpland(n) &&  n.childNodes.length > 0 ){//是否展开
				nodeDisplay = "";
				clickImg =  "<img align='left' onclick=\"dload('" + currentNodeID + "','" +queryattributevalue+"')\" id='" + imgIDtag + currentNodeID + "' src='" + endMinusImgSrc + "' border='0'>" ;
			}else{
				nodeDisplay = "none";
				clickImg =  "<img align='left' onclick=\"dload('" + currentNodeID + "','" +queryattributevalue+"')\" id='" + imgIDtag + currentNodeID + "' src='" + endPlusImgSrc + "' border='0'>" ;
			}
		}else{
			nodeDisplay = "none";
			clickImg = "<img align='left'  src='" + endNodeImgSrc + "' border='0'>" ;;
		}

		html += "<tr><td id='imgtd_" + currentNodeID + "'>";
		html += clickImg ;
		html += getCheckboxHTML(n);
		html += "</td><td nowrap>" ;
		html += img + name + "</td></tr>"; 

		html += "<tr style='display:" + nodeDisplay + "' id='" + tdIDtag + currentNodeID + "'><td" ;
			//html += " background='"+background+"' " ;
		html += ">&nbsp;</td>" ;
		html += "<td id='cc_" + currentNodeID +  "'>";
		html += parseNode( n ) ;
		html += "</td></tr>";

	//
	html += "</table>"; 
	return html;
}

function dload(id,att){
	menu(id);
	var tdObj = eval( "cc_" + id );
	if( tdObj.innerHTML != "" ) return ;

	var imgtdObj = eval( "imgtd_" + id );

	request = new ActiveXObject("microsoft.xmlhttp");
	var url = dynamicxmlsourcepage + "?" + queryattribute +"="+ att; 
	request.open( "GET", url , false );
	request.send("");
	
	if(request.status != 200)
	{
		var win = window.open("");
		win.document.write(request.responseText);
		tdObj.innerHTML = "服务器错误";
		//alert(request.responseText);
		request = null; 
		return "";
	}

	var trObj = eval( "cc_" + id );

	var clickImgObj = document.images[ imgIDtag + id ] ;

	//处理未取消的事件
	//if( clickImgObj.src.indexOf(nodeImgSrc) > -1 || clickImgObj.src.indexOf(endNodeImgSrc) > -1 ) return ;
 
	thexml = request.responseXML ;
	if(thexml==null||thexml.xml==""){
		alert( "错误！非xml文档！" );
		return ;
	}
	var root = thexml.documentElement ;
	if( root.childNodes.length == 0 ){
		
		if( showline == "true" ){
			//改变图片 
			if( clickImgObj.src.indexOf(endMinusImgSrc) > -1  ) clickImgObj.src = endNodeImgSrc ;
			else clickImgObj.src = nodeImgSrc ;
			clickImgObj.onclick = ""; //取消click事件
		}
		else
			clickImgObj.style.display = "none"; //隐藏加载图片
		tdObj.innerHTML = "";
		var trObj = eval( tdIDtag + id );
		trObj.style.display = "none";
		return ;
	}
	
	setNodeID(xmlDoc,thexml.documentElement);

	var n = getNodeByID(id);
	appendNodes( thexml.documentElement , n );

	if( showline == "true" )
		tdObj.innerHTML= parseNodeWithLine(thexml.documentElement);
	else
		tdObj.innerHTML= parseNode(thexml.documentElement);
	request = null; 
}

//将新加载的xml加入xmlDoc
function appendNodes( node , toNode ){
	var loadedNode, newNode , newAtt ;
	var i,j;
	for( i = 0 ; i < node.childNodes.length ; i ++ ){
		loadedNode = node.childNodes[i];
		newNode = xmlDoc.createNode( 1 , "node", "");
		for(j=0;j<loadedNode.attributes.length;j++){
			newAtt = xmlDoc.createAttribute( loadedNode.attributes[j].name );
			newAtt.value = loadedNode.attributes[j].value ;
			newNode.attributes.setNamedItem( newAtt ) ;
		}
		if( loadedNode.childNodes.length > 0 ) appendNodes( loadedNode, newNode); //第归
		toNode.appendChild( newNode );
	}
}



//**********************************************************

function getImageAttValue( node ){
	return getAttValue( node , "image" );
}

function getNameAttValue( node ){
	return getAttValue( node , "name" );
}

function getAttValue( node , name ){
	return node.getAttribute(name);
}

/*
function getNodeClickFunction( node, minRangeID, maxRangeID ){
	var f = "nodeClick(" + node.getAttribute("nodeID") + "," +
			"'" + (parseInt(minRangeID)) +","+ (parseInt(maxRangeID)) + "',";
	var j = 0;
	for(j=0;j<node.attributes.length;j++ ){
		f += "'"+node.attributes[j].value+"'" ;
		if(j!=node.attributes.length-1) f+=",";
	}
	f+=")";
	return f;
}*/

//节点是否展开
//只满足一级搜索
function isExpland( node ){
	var expland = node.getAttribute("expland") ;
	//if( expland == null ) 
	if( expland == "true" ) return true ;
	//var temp = node.selectSingleNode( "*[@expland='true']" );
	//if( temp != null ) return true;
	//else return false;
	var i;
	var childList = node.getElementsByTagName("node");
	for(i=0;i<childList.length;i++){
		if(childList[i].getAttribute("expland")== "true" ) return true ;
	}
	if( expland == null && defaultexpland != null ){
		return ( defaultexpland == "true" ) ? true : false ;
	}
	return false ;
}

/*******************************
*
*开发函数
*
*
*******************************/
/*
function nodeClick(){
 
	var j = 0;
	var argList = "nodeClick(";
	for( j=0;j<arguments.length;j++ ){
		argList += "'" + arguments[j] + "'";
		if(j!=arguments.length-1) argList += "," ;
	}
	argList += ")";
	alert( "节点单击事件函数：\n" + argList + "\n 第一个参数为节点序号，第一个参数为子节点序号范围,后面依次为节点属性值。\n arguments对象可获取所有参数:\n arguments.length ; arguments[0] \n\n[XmlTreeParser 0.1 jianyi0115@163.com] " );
 
	// ChangeNodeDisplay( arguments[0] ) ;

	var n = getNodeByID( arguments[0] , nodeList ) ;
	unCheckParent( n );
}*/

//选中节点id指定的节点的所有父节点
function checkParentNodesByID( id ){
	var n = getNodeByID( id ) ;
	checkParentNodes(n,true);
}

//选中节点id指定的节点的所有子节点
function checkChildNodesByID( id ){
	var n = getNodeByID( id ) ;
	checkChildNodes(n,true);
}

//选择所有父节点
function checkParentNodes( node ,  checked ){
	if( node == xmlDoc.documentElement ) return ;
	if( node == null || node.parentNode == null || node.getAttribute("nodeID") == 1 ) return ;
	var nID = node.parentNode.getAttribute("nodeID");
	
	//var o = Form1.item("checkbox_"+ nID );
	//var o = document.getElementByName( "checkbox_"+ nID ); 
	checkCheckboxObj( nID , checked );
	//if( o != null && o.type == "checkbox" ) o.checked = checked ;
	checkParentNodes( node.parentNode , checked );
}
//选择所有所有子节点
function checkChildNodes( node , checked  ){
	if( node == null  ) return ;
	for( var i = 0; i < node.childNodes.length ; i ++ ){ 
		n = node.childNodes[i];
		var id = n.getAttribute("nodeID");
		checkCheckboxObj( id , checked );
		checkChildNodes( n , checked );
	}
}
//获取复选框对象
function getCheckboxObj(id){
	return Form1.item( "checkbox_" + id );
}
//设置复选框的选择状态
function checkCheckboxObj(id,checked){
	var o = Form1.item( "checkbox_" + id );
	if( o == null ) return ;
	o.checked = checked ;
}
 //改变节点的折叠状态
function changeNodeDisplay( id ){
	menu(id);
}
//保持自节点与父节点的继承选择
function keepChildNodesCheckedStatus( id ){
	var node = getNodeByID(id) ;
	var obj = getCheckboxObj( id ) ;
		 
	checkChildNodes( node , obj.checked );
}	

//获取体现当前数状态的xml字符串
function getFullStateXml( ){
	makeStateXml(  xmlDoc.documentElement );
	return xmlDoc.xml ;
}

//将xmlDoc的数据与数状态同步
function makeStateXml(node){
	//if( node == null ) node = xmlDoc.documentElement ;

	var i , j ;
	var objTD,objCheckbox;
	var name,id;
	var n ;
	for( i = 0; i < node.childNodes.length ; i ++ ){ 
		n = node.childNodes[i];
		makeStateXml(n);

		id = n.getAttribute("nodeID") ;
		for( j=0;j<n.attributes.length;j++ ){
			name = n.attributes[j].name ;
			switch( name ){
				case "expland" :
					try{
					   objTD = eval( tdIDtag + id );	
					}catch(e){break;}
					if( objTD == null ) break ;
					if( objTD.style.display == "none" )
						n.attributes[j].value = "false";
					else
						n.attributes[j].value = "true";
				break;
				case "checked" :
					objCheckbox = getCheckboxObj(id);
					if( objCheckbox == null ) break ;
					n.attributes[j].value = "" + objCheckbox.checked;
				break ;
				default :
				break ;	
			}
		}
	}
	
}

//重新设置选择状态
//keyAttribute 设置依据字段
//valueList   以","间隔的值列表
function setCheckedStatus(keyAttribute,valueList){
	if( valueList == null || valueList == "" ) return ;
	valueList = "," + valueList + ",";
	var allList =xmlDoc.getElementsByTagName("*"); //避免第归
	var attValue;
	for(var i = 0 ; i < allList.length; i++ ){
		attValue = allList[i].getAttribute( keyAttribute );
		if( attValue == null ) continue ;
		if( valueList.indexOf( "," + attValue + "," ) != -1 ){
			checkCheckboxObj( allList[i].getAttribute("nodeID") , true );
		}	
	}
}

//清除所有节点的选择状态
function clearCheckedStatus(){
	var allList =xmlDoc.getElementsByTagName("node"); //避免第归
	var cObj;
	for(var i = 0 ; i < allList.length; i++ ){
		checkCheckboxObj( allList[i].getAttribute("nodeID") , false );

	}
}

//获取选择的节点的某个属性值列表
//keyAttribute 要获取的属性名
function getCheckedValueList(keyAttribute){

	var allList =xmlDoc.getElementsByTagName("*"); //避免第归
	var attValue;
	var cObj;
	var list = "";
	for(var i = 0 ; i < allList.length; i++ ){
		attValue = allList[i].getAttribute( keyAttribute );
		if( attValue == null ) continue ;
		cObj = getCheckboxObj( allList[i].getAttribute("nodeID") );
		if( cObj == null || cObj.checked == false ) continue ;
		if( list != "" ) list += ",";
		list += attValue ;
	}
	return list;
	
}
 

/*
function XmlTree(a){
	this.Parse = function( xmlIslandObj , divObjID ){
		alert();
	}
}
*/

// for test 

//window.onload= parseTreeXml;
/*
window.onunload = function(){
	nodeList = null;
	xmlDoc = null;
}*/

function PreloadImages()
{ 
  var args = PreloadImages.arguments;
  document.imageArray = new Array(args.length);
  for(var i=0; i<args.length; i++)
  {
    document.imageArray[i] = new Image;
    document.imageArray[i].src = args[i];
  }
}
